import { _decorator, Component, Node, Prefab, instantiate, Vec3 } from 'cc';
import { Battle } from '../Battle';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
import { ResourcesUtil } from '../ResourcesUtil';
const { ccclass, property } = _decorator;

@ccclass('EffectManager')
export class EffectManager extends Component {

    start() {
        GameAPI.RegGEvent(EnEvent.CreateEffectEvent, this.OnCreateEffectEvent.bind(this), this);
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnCreateEffectEvent(arg) {
       ResourcesUtil.loadPrefabs("Effect/" + arg.effectName).then((prefab) => {
            this.CreatorEffect(prefab, arg.position, arg.scale, arg.parentNode);
        });
    }

    CreatorEffect(prefab: Node, pos: Vec3, scale: Vec3, parent:Node) {
        let obj = instantiate(prefab);
        if (parent == null) {
            Battle.ContentParent.addChild(obj);
        }
        else{
            parent.addChild(obj);
        }
        obj.setPosition(pos);
        if (scale) {
            obj.setScale(scale);
        }
        // obj.setPosition(new Vec3(0, 0, 0));
        obj.active = true;
    }



}


